package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.models.MultiTareSalesRpt;

public class MultiTareSalesRptService {
	private DatabaseHelper DBHelper;
	private MessageDialog mMessageDialog;
	Context context;
	
	public MultiTareSalesRptService(){}
	
	public MultiTareSalesRptService(Context context){
		this.context = context;
		this.DBHelper = new DatabaseHelper(context);
	}
	
	public void DeleteAll() {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+ DatabaseSchema.MultiTareSalesRpt.SCHEMA_NAME);
	}
	
	public void DeleteByID(long id) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+ DatabaseSchema.MultiTareSalesRpt.SCHEMA_NAME
				+" WHERE "+ DatabaseSchema.MultiTareSalesRpt.MT_ID +" = ?",
				new Object[] { id });
	}
	
	public boolean CheckMultiTareInList(String mt_upc_plu,int mt_tare_no,String date){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		
		Cursor cursor = db.rawQuery("select mt_tare_no from " + DatabaseSchema.MultiTareSalesRpt.SCHEMA_NAME
				+ " where mt_tare_no = " + mt_tare_no + " and mt_upc_plu = '"+ mt_upc_plu +"' and mt_date = '"+ date +"'", null);
		if (cursor == null) {
			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return false;
		}
		if (cursor.moveToNext()) {
			cursor.close();
			return true;
		}
		cursor.close();
		return false;
	}
	
	public void Update(MultiTareSalesRpt obj){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"Update " + DatabaseSchema.MultiTareSalesRpt.SCHEMA_NAME + " set mt_date = ?,mt_qty = mt_qty + ?," +
						"mt_upc_plu = ?,update_datetime = strftime('%Y%m%d%H%M%S','now')" +
						" where mt_upc_plu = ? and mt_tare_no = ? and mt_date = ?",
				new Object[] { obj.getMt_date(), obj.getMt_qty(), obj.getMt_upc_plu(), 
						obj.getMt_upc_plu(), obj.getMt_tare_no(), obj.getMt_date() });
	}
	
	public void InsertOrUpdate(MultiTareSalesRpt obj){
		if(CheckMultiTareInList(obj.getMt_upc_plu(), obj.getMt_tare_no(), obj.getMt_date())){
			this.Update(obj);
		}else {
			this.Insert(obj);
		}
	}
	
	public List<MultiTareSalesRpt> FindAll(){
		List<MultiTareSalesRpt> obj = new ArrayList<MultiTareSalesRpt>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		
		Cursor cursor = db.rawQuery("select * from " + DatabaseSchema.MultiTareSalesRpt.SCHEMA_NAME, null);
		if (cursor == null) {
			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int mt_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.MultiTareSalesRpt.MT_ID));
			int mt_store_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.MultiTareSalesRpt.MT_STORE_ID));
			int mt_station = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.MultiTareSalesRpt.MT_STATION));
			String mt_date = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MultiTareSalesRpt.MT_DATE));
			String mt_upc_plu = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MultiTareSalesRpt.MT_UPC_PLU));
			int mt_tare_no = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.MultiTareSalesRpt.MT_TARE_NO));
			int mt_qty = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.MultiTareSalesRpt.MT_QTY));
			
			obj.add(new MultiTareSalesRpt(mt_id, mt_store_id, mt_station, mt_date, mt_upc_plu, mt_tare_no, mt_qty));
		}
		cursor.close();
		return obj;
	}
	
	public void Insert(MultiTareSalesRpt obj){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"INSERT INTO " + DatabaseSchema.MultiTareSalesRpt.SCHEMA_NAME + " VALUES(?,?,?,?,?,?,?," +
						"strftime('%Y%m%d%H%M%S','now'),strftime('%Y%m%d%H%M%S','now'))",
				new Object[] { null, obj.getMt_store_id(), obj.getMt_station(), obj.getMt_date(), obj.getMt_upc_plu()
						, obj.getMt_tare_no(), obj.getMt_qty() });
	}
	
	public List<MultiTareSalesRpt> getMultiTareSalesRptsByDate(String Date){
		List<MultiTareSalesRpt> list = new ArrayList<MultiTareSalesRpt>();
//		String sql = "select * from t_multi_tare_sales_rpt where mt_date = '" + Date + "' order by mt_id";
		String sql = "select mt_tare_no,mt_qty,F01,F02 from t_multi_tare_sales_rpt,t_item_setting where mt_date = '" + Date + "' and F01 = mt_upc_plu order by mt_id";
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if(cursor == null) return list;
		while (cursor.moveToNext()) {
			MultiTareSalesRpt item = new MultiTareSalesRpt();
			item.setMt_tare_no(cursor.getInt(0));
			item.setMt_qty(cursor.getInt(1));
			item.setMt_upc_plu(cursor.getString(2) + "  " + cursor.getString(3));
			list.add(item);
		}
		cursor.close();
		return list;
	}
	
	public String getTareName(String TareId){
		String sql = "select ta_desc from t_tare_setting where ta_no = " + TareId;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if(cursor == null) return "";
		if (cursor.moveToNext()) {
			String name = cursor.getString(0);
			cursor.close();
			return name;
		}
		return "";
	}
	
	public String getItemName(String PLU){
		String sql = "select F02 from t_item_setting where F01 = '" + PLU + "'";
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if(cursor == null) return "";
		if (cursor.moveToNext()) {
			String name = cursor.getString(0);
			cursor.close();
			return name;
		}
		return "";
	}
	
	public void ShowMessage(String text, int type, int icon) {
		mMessageDialog = new MessageDialog(context);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener() {

		@Override
		public void onClickYes() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onClickNo() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onClickOK() {
			// TODO Auto-generated method stub
			mMessageDialog.dismiss();
		}
	};
}
